#!/usr/bin/perl
#Creates 10 different testsuites each with 1 sliced used for testing and 9 as training data
#After this run_taggers.pl will have to be called to run all of them through the taggers and log the results.

use File::Copy;
use strict;
use warnings;
use encoding 'utf8';
use utf8;
use feature 'unicode_strings';

open STDERR, '>make_testsuites_errors_log.txt' or die $!;#LOG all errors to a file
my $i=0;
my $n=0;
my $t=0;


#Copy slices and make testslice for each testsuite
for ($i = 0; $i < 10; $i++) {
	for ($n = 0; $n < 10; $n++) {
		#&copy_slice($n,$i,"tt","");
		#&copy_slice($n,$i,"cc","");
		&copy_slice($n,$i,"tt","_gold");
		&copy_slice($n,$i,"cc","_gold");
	}
	#&make_test_slice($i,"tt","");
	#&make_test_slice($i,"cc","");
	&make_test_slice($i,"tt","_gold");
	&make_test_slice($i,"cc","_gold");
	$t++;
}
print "Slices created.\n";


#Move testslices to right place
for ($i = 0; $i < 10; $i++) {
	#&cleanup_after($i,"tt",""); #move testfile
	#&cleanup_after($i,"cc",""); #move testfile
	&cleanup_after($i,"tt","_gold"); #move testfile
	&cleanup_after($i,"cc","_gold"); #move testfile
}
print "Slices moved.\n";
close STDERR;
print "DONE";

##############################################
#SUBS
#copy slices around so that each testsuite gets one testslice and the others as training
sub copy_slice{ #$n = slicenumber (0.9) =$i = testnumber(0-9), $set=(cc|tt), $gold="" or "_gold";
	my $n=$_[0];
	my $i=$_[1];
	my $set=$_[2];
	my $gold=$_[3];
		my $filetobecopyied = "./slice$gold".$n.".".$set;
		my $newfile = "./test$i.$set$gold/slice$n$gold.$set";
		copy($filetobecopyied, $newfile) or die "Slice $filetobecopyied cannot be copied to $newfile.";
		print "Copied slice $filetobecopyied to testsuite $newfile\n";
}

sub make_test_slice{ #Makes a testslice for each testsuite only containing tokens and no tags
	my $i=$_[0];
	my $set=$_[1];
	my $gold=$_[2];
	open IN, "<:utf8", "./slice".$gold."$i".".$set" or die $!; #FIXME? UNICODE HELL
	open OUT, ">:utf8", "./test$i.$set$gold/slice$gold"."_test".$i.".".$set or die $!;
		while (<IN>) {
			my $token="NULL";#this should not happen;
			if ($set eq "tt"){#For the TNT Testsuites (one token / tag pair per line seperated by tab)
				if ($_ =~ m/.*\t.*/){	#if we can split the token is the first thing
					my @fields=split(/\t/, $_,); #Split on Tabs
					$token=$fields[0]; #Get only  token
				}
				else{#if we can't we have the token. (end of sentence?)
					$token="";
				}
	  			print OUT $token."\n";#print token to file but not tag
	  			#print "Printed $token to testslice $n of suite $i\n";
			}
			else{# For the CC Testsuites (one sentence per line. token / tag pairs seperated by |)
				my @words=split(/\ /,$_);#put words in array
				foreach (@words){
						my @fields=split(/\|/,$_);#split on |
						$token=$fields[0]; #put only token in testfile
				print OUT $token." ";#print token to file but not tag
				}
				print OUT "\n"# End of sentence
			}
		}
	close OUT;
	close IN;
	print "Made Test slice ./test$i.$set$gold/slice$gold"."_test".$i.".$set"." for testsuite ./slice$gold$i$set\n";
}

sub cleanup_after{
	my $i=$_[0];
	my $set=$_[1];
	my $gold=$_[2];
	my $filetobemoved = "./test$i.$set$gold/slice$gold"."_test".$i	.".".$set;
	my $setn=substr($set, 0, 1);
	my $newfile = "./test$i.$set$gold/suite$i$gold.$setn";
	move($filetobemoved, $newfile) or die "Slice $filetobemoved cannot be moved to $newfile";
	print "Moved slice $filetobemoved to testsuite $newfile\n";
	my $oldfile= "./test$i.$set$gold/slice$i$gold.$set";
	if (unlink($oldfile)) {
		print "File $oldfile deleted successfully.\n";
	} 
	else {#This should not happen
    	print "File $oldfile was not deleted.\n";
	}
	#Merge slices to suite
	system "cat ./test$i.$set$gold/*.$set > ./test$i.$set$gold/suite$i$gold.$set";
	#remove slices
	system "rm ./test$i.$set$gold/slice*";
}


#TODO:
#Double check everything here
#Make comments